<?php
	// this file is where ALL basic functions are defined
	
	function mysql_prep( $value ) 
	{
		$magic_quotes_active = get_magic_quotes_gpc();
		$new_enough_php = function_exists( "mysql_real_escape_string" ); // i.e. PHP >= v4.3.0
		if( $new_enough_php ) 
		{ 
			// undo any magic quote effects so mysql_real_escape_string can do the work
			if( $magic_quotes_active ) 
			{
				 $value = stripslashes( $value ); 
			}
			$value = mysql_real_escape_string( $value );
		} 
		else 
		{ // before PHP v4.3.0
			// if magic quotes aren't already on then add slashes manually
			if( !$magic_quotes_active ) 
			{
				 $value = addslashes( $value );
			}
			// if magic quotes are active, then the slashes already exist
		}
		return $value;
	}
	
	function redirect_to( $location = NULL ) 
	{
		if ($location != NULL) 
		{
			header("Location: {$location}");
			exit;
		}
	}
	
	function confirm_query($result)
	{
		if(!$result)
		{
			die("Database query failed!". mysql_error());
		}
	}
	
	function get_all_subjects($public = true)
	{
		global $connection;
		$query = "SELECT * 
				  FROM subjects ";
				  
		if ($public) 
		{
			$query .= "WHERE visible = 1 ";
		}
		
		$query .= "ORDER BY position ASC";
		$subject_set = mysql_query($query, $connection);
		confirm_query($subject_set);
		return $subject_set;
	}
	
	function get_all_users()
	{
		global $connection;
		$query = "SELECT * 
				  FROM users 
				  ORDER BY username ASC";
				  
		$users = mysql_query($query, $connection);
		confirm_query($users);
		return $users;
	}
	
	function display_all_user_info($users)
	{
		// give me a row from the users table and traverse through each row(obj) 
		while($user = mysql_fetch_array($users))
		{
			// get the attributes in columns 1 & 2 ---> username & fullname
			echo $user['username'] 
				." 	". $user['fullname'] 
				." 	". $user['emailaddr'] 
				." 	". $user['hashed_password'] 
				." 	". $user['salt'] 
				." 	". $user['privilege']
				. " ". "<br />";
		}
	}
	
	//use the below function as a template for getting all the accounts, account holders and other getters
	function get_pages_for_subject($subject_id)
	{
		global $connection;
		$query = "SELECT * 
				  FROM pages 
				  WHERE subject_id= {$subject_id} 
				  ORDER BY position ASC";
		
		$pages = mysql_query($query, $connection);
		confirm_query($pages);
		return $pages;
	}
	 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// FORM FUNCTIONS
	//
	function check_required_fields($required_array) 
	{
		$field_errors = array();
		foreach($required_array as $fieldname) 
		{
			if (!isset($_POST[$fieldname]) || (empty($_POST[$fieldname]) && $_POST[$fieldname] != 0)) 
			{ 
				$field_errors[] = $fieldname; 
			}
		}
		return $field_errors;
	}
	
	function check_max_field_lengths($field_length_array)
	 {
		$field_errors = array();
		foreach($field_length_array as $fieldname => $maxlength ) 
		{
			if (strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength) 
			{
				 $field_errors[] = $fieldname; 
			}
		}
		return $field_errors;
	}
	
	function display_errors($error_array) 
	{
		echo "<p class=\"errors\">";
		echo "Please review the following fields:<br />";
		foreach($error_array as $error) 
		{
			echo " - " . $error . "<br />";
		}
		echo "</p>";
	}
?>